package com.swgk.core.viewpager;

import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swgk.core.util.ResolutionUtil;

/**
 * Description:
 * Author zhengkewen
 * Time 2017/8/2 14:25
 */
public class SlidingHorizontalScrollView extends HorizontalScrollView {
    private int mScreenWidth = 0;
    private int mItemWidth;
    private LinearLayout tabLinearLayout;
    private int tabStyle;
    private int tabColor;
    private int columnSelectIndex = 0;
    private ViewPager viewPager;

    public SlidingHorizontalScrollView(Context context) {
        this(context, null);
    }

    public SlidingHorizontalScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlidingHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public SlidingHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setTabStyle(int tabStyle) {
        this.tabStyle = tabStyle;
    }

    public void setTabColor(int tabColor) {
        this.tabColor = tabColor;
    }

    public void setViewPager(final ViewPager viewPager) {
        this.viewPager = viewPager;
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void init() {
        mScreenWidth = ResolutionUtil.getScreenWidth(getContext());
        mItemWidth = mScreenWidth / 4;
        tabLinearLayout = new LinearLayout(getContext());
        tabLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        addView(tabLinearLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void addTab(String tagName, int index) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mItemWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        TextView textView = new TextView(getContext());
        textView.setId(index);
        textView.setTextAppearance(getContext(), tabStyle);
        textView.setGravity(Gravity.CENTER);
        textView.setText(tagName);
        textView.setTextColor(getResources().getColorStateList(tabColor));
        if (columnSelectIndex == index) {
            textView.setSelected(true);
        }
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < tabLinearLayout.getChildCount(); i++) {
                    View localView = tabLinearLayout.getChildAt(i);
                    if (localView != view)
                        localView.setSelected(false);
                    else {
                        localView.setSelected(true);
                        viewPager.setCurrentItem(i);
                    }
                }
            }
        });
        tabLinearLayout.addView(textView, index, params);
    }

    public void selectTab(int tab_postion) {
        columnSelectIndex = tab_postion;
        for (int i = 0; i < tabLinearLayout.getChildCount(); i++) {
            View checkView = tabLinearLayout.getChildAt(tab_postion);
            int k = checkView.getMeasuredWidth();
            int l = checkView.getLeft();
            int i2 = l + k / 2 - mScreenWidth / 2;
            smoothScrollTo(i2, 0);
        }
        //判断是否选中
        for (int j = 0; j < tabLinearLayout.getChildCount(); j++) {
            View checkView = tabLinearLayout.getChildAt(j);
            boolean ischeck;
            if (j == tab_postion) {
                ischeck = true;
            } else {
                ischeck = false;
            }
            checkView.setSelected(ischeck);
        }
    }

    @Override
    protected void onScrollChanged(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
        super.onScrollChanged(paramInt1, paramInt2, paramInt3, paramInt4);
    }

}
